using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using DiscMaster.Web.Models;

namespace DiscMaster.Web.Areas.Clubs.Controllers
{
    public class ClubsController : Controller
    {
        private IDiscMasterRepository repository;

        public ClubsController()
            : this(new DiscMasterRepository()){
        }

        public ClubsController(IDiscMasterRepository controllerrepository)
        {
            repository = controllerrepository;
        }

        //
        // GET: /Clubs/

        [OutputCache(CacheProfile = "ShortCache", VaryByParam="none")]
        public ActionResult Index()
        {
            var clubs = repository.FindAllClubs().ToList();
            if (clubs.Count==0)
            {
                return View("NoData");
            }
            else
            {
                return View(clubs);
            }
        }

        //
        // GET: /Clubs/Details/name

        [OutputCache(CacheProfile = "MediumCache", VaryByParam="id")]
        public ActionResult Details(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                DiscMaster.Web.Models.Club club = repository.GetClubByName(id);
                if (club == null)
                {
                    return View("NotFound");
                }
                return View("Details", club);
            }
            else
            {
                return View("NotFound");
            }
        }

        //
        // GET: /Clubs/Create

        [Authorize()]
        public ActionResult Create()
        {
            var club = new DiscMaster.Web.Models.Club();
            return View(club);
        } 

        //
        // POST: /Clubs/Create

        [Authorize()]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(DiscMaster.Web.Models.Club club)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    club.clubid = Guid.NewGuid();
                    UpdateModel(club);
                    if (repository.GetClubByName(club.name)!=null)
                    {
                        ModelState.AddModelError("name", "The specified name already exists");
                        return View(club);
                    }
                    club.createdby = repository.GetLoggedOnPlayer().UserId;
                    club.ClubMembers.Add(new DiscMaster.Web.Models.ClubMember() { clubid = club.clubid, playerid = repository.GetLoggedOnPlayer().UserId, owner=true });
                    repository.AddClub(club);

                    repository.Save();

                    return RedirectToAction("Details", new { id = club.clubid });
                }
                catch (Exception ex)
                {
                    ViewData["Error"] = ex.Message;
                    return View(club);
                }
            }
            return View(club);
        }

        //
        // GET: /Clubs/Edit/5
        [Authorize()]
        public ActionResult Edit(string id)
        {
            var club = repository.GetClub(id);
            if (club == null)
            {
                return View("NotFound");
            }
            if (repository.GetLoggedOnPlayer().UserId != club.createdby)
            {
                return View("OnlyOwner");
            }
            return View(club);
        }

        //
        // POST: /Clubs/Edit/5

        [Authorize()]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id, FormCollection collection)
        {
            var club = repository.GetClub(id);
            if (club == null)
            {
                return View("NotFound");
            }
            if (repository.GetLoggedOnPlayer().UserId != club.createdby)
            {
                return View("OnlyOwner");
            }
            try
            {
                UpdateModel(club);

                repository.Save();

                return RedirectToAction("Details", new { id = club.clubid });
            }
            catch
            {
                return View(club);
            }
        }
    }
}
